package db;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import entity.Card;
import entity.Member;


public class iyoga {
	
	//数据库的连接
	private Connection conn;
	//执行sql的Statement
	private Statement stmt;
	
	/**
	 * @param member 加入的会员
	 * @return void
	 */
	public void addMember(Member member){
		String name = member.getName();
		String address = member.getAddress();
		String job = member.getAddress();
		try{
			//加载数据库驱动
			Class.forName("com.mysql.jdbc.Driver");
			//获得数据库的连接
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8",
					"root",
					"");
			//获得执行sql的Statement
			stmt = conn.createStatement();
			//执行SQL的结果集
			ResultSet rs ;
			rs = stmt.executeQuery("select * from member;");
			rs.last();
			
			//加入的会员的会员编号
			int memberId = rs.getRow() + 1;
			//把会员信息插入会员表
			stmt.execute("insert into member (memberId,name,address,job) values ("+ memberId +
					  "," + '\'' + name + '\'' +","+ '\'' + address+ '\'' + ","+ '\'' + job+  '\'' +
					  ");");
			System.out.println("OK. " + name + "'s member id is "+ ("00"+memberId).substring(("00"+memberId).length()-3)+".");
		}catch(SQLException sqle){
			sqle.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
		}
				
	}
	
	
	/**
	 * 
	 * @param card 要办的会员卡的实例
	 * @param memberName 要办会员卡的会员名字
	 * @return void
	 */
	public void addCard(Card card,String memberName){
		//会员名字
		String name = memberName;
		//卡型号
		String cardType = null;
		//卡剩余的使用次数
		int times;
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/test?useUnicode=true&character=utf-8",
					"root",
					"");
			stmt = conn.createStatement();
			//获得卡型号
			cardType = card.getCardType();
			//SQL结果集
			ResultSet rs ;
			//获得card中所有的记录
			rs = stmt.executeQuery("select * from card;");
			//指针移到最后
			rs.last();
			//确定下一条插入记录的cardId
			int cardId = rs.getRow() + 1;
			//得到所办卡的剩余次数
			times = card.getLastTime();
			
			//如果次数为空即是年卡，否则是次卡
			if(times != 0){
				stmt.execute("insert into card (cardId,cardType,lastTimes) values ("+ cardId + "," + '\'' + cardType + '\'' + "," + times +");");
			} else {
				stmt.execute("insert into card (cardId,cardType) values ("+ cardId + "," + '\'' + cardType + '\'' +");");
			}
			
			
		System.out.println(name);
			//查询名字为name的会员信息
			rs = stmt.executeQuery("select * from member where name= \'"+"张三"+"\';");
			rs.next();
System.out.println(rs.getRow());
			//获得会员的会员编号
			int memId = rs.getInt(1);
			/*if(rs.next()){
				memId = rs.getInt(1);
			}*/
System.out.println("insert into member_information (cardId,memberId,cardType,lastTimes) values (" + cardId + "," + memId + "," + '\'' + cardType + '\'' + "," + times + ");");
			//如果times为0，不需要插入剩余使用次数，否则需要插入剩余次数
			if(times == 0){
				stmt.execute("insert into member_information (cardId,memberId,cardType) values ("+ cardId + "," + memId +","+ "'" + cardType + "'" +");");
				System.out.println("OK. A new " + cardType + " add to " + name + "("+("00"+memId).substring(("00"+memId).length()-3)+").");
			} else {
				stmt.execute("insert into member_information (cardId,memberId,cardType,lastTimes) values (" + cardId + "," + memId + "," + '\'' + cardType + '\'' + "," + times + ");");
				System.out.println("OK. A new " + cardType + " with " + times + " added to "+name + ".");
			}
			
		}catch(SQLException sqle){
			sqle.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
		}
	}
	public static void main(String args[]) {
		String argName = args[0].substring(0,3)+args[0].substring(4,5).toUpperCase() + args[0].substring(5);
		iyoga manage = new iyoga();
		Method addMember = null;
		Member member ;
		Card card ;
		try {
			if(argName.equals("addMember")){
				member = new Member(args[1],args[2],args[3]);
				addMember = iyoga.class.getDeclaredMethod(argName, Member.class);
				addMember.invoke(manage, member);
			} else if(argName.equals("addCard")){
				
				addMember = iyoga.class.getDeclaredMethod(argName, Card.class,String.class);
				int param;
				if(args.length < 4){
					param = 0;
				} else {
					param = Integer.parseInt(args[3]);
				}
				card = new Card(args[2],param);
				addMember.invoke(manage, card,args[1]);
			}
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
	}
}
